package com.core.visaInfo.bo;

import java.util.List;

import javax.annotation.Resource;
import javax.enterprise.inject.New;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.core.journey.dao.JourneyDao;
import com.core.journey.dto.JourneySearchCondition;
import com.core.journey.pojo.Journey;
import com.core.role.dao.RoleDao;
import com.core.role.pojo.Role;
import com.core.user.pojo.User;
import com.core.visaInfo.dao.VisaInfoDao;
import com.core.visaInfo.dto.VisaInfoSearchCondition;
import com.core.visaInfo.pojo.VisaInfo;
import com.core.visitor.dao.VisitorDao;
import com.core.visitor.pojo.Visitor;
@Service
public class VisaInfoBo {
	protected Logger log = Logger.getLogger(this.getClass());
	@Resource
	private VisaInfoDao visaInfoDao;
	@Resource
	private VisitorDao visitorDao;
	@Resource
	private JourneyDao journeyDao;
	@Resource 
	private RoleDao roleDao;
	public VisaInfo getById(VisaInfo visaInfo) {
		return visaInfoDao.getById(visaInfo.getId());
	}
	public void  add(VisaInfo visaInfo) {
		visaInfoDao.add(visaInfo);
	}
	public void update(VisaInfo visaInfo) {
		visaInfoDao.update(visaInfo);
	}
	public void del(VisaInfo visaInfo) {
		List<Visitor> delVisitors = visitorDao.getVisitorList(visaInfo.getId());
		JourneySearchCondition delSC= new JourneySearchCondition();
		delSC.setVisaInfoId(visaInfo.getId());
		List<Journey>delJourneys = journeyDao.getJourneyList(delSC);
		for (Journey journey : delJourneys) {
			journeyDao.del(journey);
		}
		for (Visitor visitor : delVisitors) {
			visitorDao.del(visitor);
		}
	
		visaInfoDao.del(visaInfo);
	}
	public List<VisaInfoSearchCondition> searchPojoList(VisaInfoSearchCondition sc,User userInfo) {
		List<Role> roles =roleDao.searchPojoByUserId(userInfo);
		int isAdmin =0;
//		判断是否是管理员
		for (Role role : roles) {
			if (role.getId()==1) {
				isAdmin=1;
			}
		}
		if (isAdmin==1) {
			return visaInfoDao.searchPojoList(sc);
		}else{
			return visaInfoDao.searchPojoListByOperator(sc);
		}
		
	}
	
	public Integer searchCnt(VisaInfoSearchCondition sc,User userInfo) {
		List<Role> roles =roleDao.searchPojoByUserId(userInfo);
		int isAdmin =0;
//		判断是否是管理员
		for (Role role : roles) {
			if (role.getId()==1) {
				isAdmin=1;
			}
		}
		if (isAdmin==1) {
			return visaInfoDao.searchCnt(sc);
		}else{
			return visaInfoDao.searchCntByOperator(sc);
		}
	}
	public void updateState(VisaInfo tempVisaInfo) {
		visaInfoDao.updateState(tempVisaInfo);
		
	}
	
}
